package pack1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;

import pack1.Generator.RandomType;

import apx.lang.AsyncChan;
import apx.lang.Chan;

public class c1 {
	public static void main(String[] args) {
		c1 case_study = new c1(); c1 case_study2 = new c1();
		//File f = new File("H:\\Program Files\\AtejiPX\\workspace\\Project1\\samples\\s1.txt");
		String fileName= "H:\\Program Files\\AtejiPX\\workspace\\Project1\\samples\\s2.txt";
		String summaryPath = "H:\\Program Files\\AtejiPX\\workspace\\Project1\\samples\\s2_results.csv";
		try
		{
			File  summaryFile = new File(summaryPath);

			FileWriter w = new FileWriter(summaryFile);
			BufferedWriter writer = new BufferedWriter(w);
			writer.write("Sample_Size,interval_1,interval_2,MAE\r\n");
			int startSize =50 ; int endSize =141; int step = 10;
			for(int sampleSize = startSize;sampleSize<endSize;sampleSize+=step)
			{
				Generator.GenerateSameplFile(sampleSize,sampleSize*5, 500,fileName,RandomType.PowerLaw,.2f);

				File f = new File(fileName);
				long time1 = System.nanoTime();
				case_study.PrepareGraph(f,false);

				long time2 = System.nanoTime();

				case_study2.PrepareGraph(f,true);System.out.println("=========== Sample Size" + sampleSize);
				System.out.println("interval 1: "+ (time2-time1));
				long time3 = System.nanoTime();

				double MAE = CalculateMeanAbsoluteError(case_study.m_nodes, case_study2.m_nodes);


				System.out.println("interval 2: "+ (time3-time2));
				System.out.println("MAE "+ MAE);
				writer.write(""+sampleSize+","+ (time2-time1)+","+ (time3-time2)+","+MAE+ "\r\n");
			}
			writer.close();
		}
		catch(IOException e)
		{
			System.out.println(e.getMessage());
		}
		System.out.println("End of Application");
	}
	static float CalculateMeanAbsoluteError(ArrayList< Node> A,ArrayList<Node> B )
	{
		float AbsoluteErrorSum = 0;
		int count = 0;
		for (int i = 0; i < A.size() && i< B.size(); i++) {
			Hashtable<Node, Float> mapA = A.get(i).GetSimRankMap();
			Hashtable<Node, Float> mapB = B.get(i).GetSimRankMap();
			//			AbsoluteErrorSum+= Math.abs((float)(A.get(i).AverageDelta - B.get(i).AverageDelta));
			//			count++;
			for(int k = 0; k <A.size() /*  the nodes in A are the same nodes in B*/; k++ )
			{
				Node temp = new Node(k);
				Float x = mapA.get(temp); Float y = mapB.get(temp);
				AbsoluteErrorSum+= Math.abs(x-y);
				count++;
			}
			//AbsoluteErrorSum += Math.abs(A[i].)
		}
		if(count>0)
			return AbsoluteErrorSum/count;
		else 
			return AbsoluteErrorSum;
	}
	ArrayList<Node> m_nodes = new ArrayList<Node>();
	public void PrepareGraph(File file, boolean heuristic_algo)
	{

		//static graph (toy example)
		//ToyGraph();
		LoadFromFile(file);
		//---------------------------------------
		for (int i = 0; i < m_nodes.size(); i++) {
			m_nodes.get(i).InitSimRank(m_nodes);
		}
		Node[] casted = getArrayFromList(m_nodes);
		//System.out.print("starting");
		//Naive_MapReduce_DeltaSimRank(casted,.85f,m_nodes.size());
		Init_Scores_NoMapReduce(casted,.85f,m_nodes.size());
		//Init_Scores_NoMapReduce(casted,.85f,m_nodes.size());
		if(!heuristic_algo)
		{
			for(int k=0;k<10;k++)
			{
				Delta_MapReduce_DeltaSimRank(casted,.85f,0,m_nodes.size());
				//DynamicallyChangeWeights(m_nodes);
			}
		}
		else 
		{
			Double incrementalDetla = null;
			freshVariable0 : for(int k=0;k<10;k++)

			{
				if(incrementalDetla==null)
					incrementalDetla = SelectivePath_Delta_MapReduce_DeltaSimRank(casted,.85f,0,m_nodes.size(),10,.2f,.3f);
				else 
				{
					if(incrementalDetla<.5)

						continue freshVariable0 ;
					else
					{
						incrementalDetla += SelectivePath_Delta_MapReduce_DeltaSimRank(casted,.85f,0,m_nodes.size(),10,.2f,.3f);

					}
				}
				//DynamicallyChangeWeights(m_nodes);
			}
		}

		//System.out.println("end");
	}
	void LoadFromFile(File file)
	{
		try
		{
			FileReader f =new FileReader(file);
			BufferedReader reader = new BufferedReader(f);
			String line;
			line=reader.readLine();//first line count of items
			int nodes_count = Integer.parseInt(line);
			m_nodes.clear();
			for(int i=0;i<nodes_count;i++)
			{
				m_nodes.add(new Node(i));
			}
			//read the edges from second line 
			while((line=reader.readLine())!=null) {
				String[] parts = line.split(",");
				int startIndex = Integer.parseInt(parts[0]); int endIndex = Integer.parseInt(parts[1]);
				float value=  Float.parseFloat(parts[0]);
				m_nodes.get(startIndex).AddInLink(m_nodes.get(endIndex), value);

			}
			//f.close();
			reader.close();
		}
		catch(FileNotFoundException e1)
		{
			System.out.println("file not found: "+e1.getMessage());
		}
		catch(IOException e2)
		{
			System.out.println(e2.getMessage());
		}
	}
	void ToyGraph()
	{
		for (int i = 0; i < 6; i++) {
			m_nodes.add(new Node(i));
		}
		m_nodes.get(0).AddInLink(m_nodes.get(1), 5);
		m_nodes.get(0).AddInLink(m_nodes.get(2), 4);

		m_nodes.get(1).AddInLink(m_nodes.get(3), 7);
		m_nodes.get(4).AddInLink(m_nodes.get(2), 4.5f);

		m_nodes.get(4).AddInLink(m_nodes.get(5), 6);
		m_nodes.get(5).AddInLink(m_nodes.get(3), 6);
	}
	Node[] getArrayFromList(ArrayList<Node> nodes)
	{
		Node[] res = new  Node[nodes.size()];
		for (int i = 0; i < res.length; i++) {
			res[i] = nodes.get(i);
		}
		return res;
	}
	void Naive_MapReduce_DeltaSimRank(Node[] graph_nodes, float C, int n)
	{
		int n2 = n;
		AsyncChan<channelPacket> naiveChannel = new AsyncChan<channelPacket>();
		{
final float C0 = C ;
final Node [ ] graph_nodes0 = graph_nodes ;
final int n20 = n2 ;
final apx . lang . AsyncChan < channelPacket > naiveChannel0 = naiveChannel ;
{
		 final java . util . List < apx . lang . gen . Branch > branches1 =new java . util . ArrayList < apx . lang . gen . Branch > ();
final apx . lang . gen . Parallel parallelBlock1 =apx . lang . gen . Parallel .getParallelBlock ();
{
apx . lang . gen . Branch branch1 = new apx . lang . gen . Branch (){
public @java . lang .Override void run () throws java . lang . Throwable {
for ( Node a : graph_nodes0) {
{
			 //System.out.println("L-1");
			 Node[] A_neighbors = a.getInNeighbors();
			 for ( Node b : graph_nodes0) {
{
				 //System.out.println("L-2");
				 float s_AB = a.getScoreForNode(b);
				 Node[] B_neighbors = b.getInNeighbors();

				 for ( Node c : A_neighbors) {
{
					 //System.out.println("L-3");
					 for ( Node d : B_neighbors) {
{
						 //System.out.println("L-4");
						 /////////////////////
						 channelPacket p = new channelPacket();
						 p.a =a;
						 p.b= b;
						 p.c = c;
						 p.d = d;
						 p.graph = graph_nodes0;
						 p.ab_score = s_AB;
						 {
final channelPacket p0 = p ;
final apx . lang . AsyncChan < channelPacket > naiveChannel1 = naiveChannel0 ;
{final java . util . List < apx . lang . gen . Branch > branches =new java . util . ArrayList < apx . lang . gen . Branch > ();
final apx . lang . gen . Parallel parallelBlock =apx . lang . gen . Parallel .getParallelBlock ();
{
apx . lang . gen . Branch branch = new apx . lang . gen . Branch (){
public @java . lang .Override void run () throws java . lang . Throwable {
naiveChannel1 . send (p0);}
}
;
branches .add (branch );
}
final apx . lang . gen . ExitStatus exitStatus =parallelBlock . run (branches );
if(exitStatus .hasReturned ()) {
throw new apx . lang . gen . ReturnException ();
} {
final Throwable throwable =exitStatus .thrownException ();
if(throwable !=null) {
if(throwable instanceof RuntimeException ) throw (RuntimeException )throwable ;
if(throwable instanceof Error ) throw (Error )throwable ;
}
}
}
						 //System.out.println(""+s_AB);
					 }
}
				 }
}


			 }
}
		 }
}
		 //for recievchannel		
		 }
for(int k=0;k<n20;k++)
		 {
			 //Combine
			 {
final float C1 = C0 ;
final apx . lang . AsyncChan < channelPacket > naiveChannel2 = naiveChannel0 ;
{final java . util . List < apx . lang . gen . Branch > branches0 =new java . util . ArrayList < apx . lang . gen . Branch > ();
final apx . lang . gen . Parallel parallelBlock0 =apx . lang . gen . Parallel .getParallelBlock ();
{
apx . lang . gen . Branch branch0 = new apx . lang . gen . Branch (){
public @java . lang .Override void run () throws java . lang . Throwable {
channelPacket x;
			 x=naiveChannel2 . receive ();

			 if(x.c==x.d)
			 {
				 x.c.setSimRank(x.d,1);
				 //System.out.println("(from " +x.c.m_Index+" to "+ x.d.m_Index + ")="+1);
			 }
			 else 
			 {
				 float currentTotal = x.c.getScoreForNode(x.d);
				 //not typical equation, but simple one
				 float result =currentTotal *  C1/(x.c.getInDegree()*x.d.getInDegree());
				 x.c.setSimRank(x.d,result);
				 //System.out.println("(from " +x.c.m_Index+" to "+ x.d.m_Index + ")="+result);

			 }
			 }
}
;
branches0 .add (branch0 );
}
final apx . lang . gen . ExitStatus exitStatus0 =parallelBlock0 . run (branches0 );
if(exitStatus0 .hasReturned ()) {
throw new apx . lang . gen . ReturnException ();
} {
final Throwable throwable0 =exitStatus0 .thrownException ();
if(throwable0 !=null) {
if(throwable0 instanceof RuntimeException ) throw (RuntimeException )throwable0 ;
if(throwable0 instanceof Error ) throw (Error )throwable0 ;
}
}
}

		 }
}
		 }
}
;
branches1 .add (branch1 );
}
final apx . lang . gen . ExitStatus exitStatus1 =parallelBlock1 . run (branches1 );
if(exitStatus1 .hasReturned ()) {
return ;
} {
final Throwable throwable1 =exitStatus1 .thrownException ();
if(throwable1 !=null) {
if(throwable1 instanceof RuntimeException ) throw (RuntimeException )throwable1 ;
if(throwable1 instanceof Error ) throw (Error )throwable1 ;
}
}
}
	}
}
	static void DynamicallyChangeWeights(ArrayList< Node> graph_nodes)
	{

		for ( Node a : graph_nodes) {
{
			//System.out.println("L-1");
			Node[] A_neighbors = a.getInNeighbors();
			for ( Node b : graph_nodes) {
{
				//System.out.println("L-2");
				float s_AB = a.getScoreForNode(b);
				int decision = (int)(Math.random()*3);
				freshVariable1 : switch(decision)
				{
				case 0:
					s_AB *=2;
					break freshVariable1 ;
				case 1:
					s_AB /=2;
				case 2:
					//no change
					break freshVariable1 ;
				}
				a.setSimRank(b, s_AB);
			}
		}
}
	}
}
	//-------------------------------------
	void Init_Scores_NoMapReduce(Node[] graph_nodes, float C, int n)
	{

		for ( Node a : graph_nodes) {
{
			//System.out.println("L-1");
			Node[] A_neighbors = a.getInNeighbors();
			for ( Node b : graph_nodes) {
{
				//System.out.println("L-2");
				float s_AB = a.getScoreForNode(b);
				Node[] B_neighbors = b.getInNeighbors();

				for ( Node c : A_neighbors) {
{
					//System.out.println("L-3");
					for ( Node d : B_neighbors) {
{
						//System.out.println("L-4");
						/////////////////////
						channelPacket p = new channelPacket();
						p.a =a;
						p.b= b;
						p.c = c;
						p.d = d;
						p.graph = graph_nodes;

						if(p.c==p.d)
						{
							p.c.setSimRank(p.d,1);
							p.ab_score = 1;
							//System.out.println("(from " +p.c.m_Index+" to "+ p.d.m_Index + ")="+1);
						}
						else 
						{
							float currentTotal = p.c.getScoreForNode(p.d);
							//not typical equation, but simple one
							float result =currentTotal *  C/(p.c.getInDegree()*p.d.getInDegree());
							p.c.setSimRank(p.d,result);

							//System.out.println("(from " +p.c.m_Index+" to "+ p.d.m_Index + ")="+result);

						}
					}
				}
}


			}
}
		}
}


	}
}


	//-------------------------------
	void Delta_MapReduce_DeltaSimRank(Node[] graph_nodes, float C, float epsilon, int n)
	{

		//the delta simrank implementation

		{
final Node [ ] graph_nodes1 = graph_nodes ;
final float C2 = C ;
final float epsilon0 = epsilon ;
{
		 final java . util . List < apx . lang . gen . Branch > branches3 =new java . util . ArrayList < apx . lang . gen . Branch > ();
final apx . lang . gen . Parallel parallelBlock3 =apx . lang . gen . Parallel .getParallelBlock ();
{
apx . lang . gen . Branch branch3 = new apx . lang . gen . Branch (){
public @java . lang .Override void run () throws java . lang . Throwable {
{
final Node [ ] graph_nodes2 = graph_nodes1 ;
final float C3 = C2 ;
final float epsilon1 = epsilon0 ;
{
final Node [ ] array = graph_nodes2; final int blockcount = java . lang . Runtime .getRuntime ().availableProcessors (); {final java . util . List < apx . lang . gen . Branch > branches2 =new java . util . ArrayList < apx . lang . gen . Branch > ();
final apx . lang . gen . Parallel parallelBlock2 =apx . lang . gen . Parallel .getParallelBlock ();
{
final apx . util . RangeInteger range = new apx . util . RangeInteger (0 , ((blockcount )- 1 ));
final int last = (range ). max ;
for(int block = (range ). min ;block <= last ;block ++ )
{
final int nbBlock = block ;
{
{
apx . lang . gen . Branch branch2 = new apx . lang . gen . Branch (){
public @java . lang .Override void run () throws java . lang . Throwable {
{
final apx . util . RangeInteger range0 = new apx . util . RangeInteger ((((new apx . util . RangeInteger ((0 ), ((array . length )- 1 ))). min )+ ((((new apx . util . RangeInteger ((0 ), ((array . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array . length )- 1 ))). min )+ 1 )/ (java . lang . Runtime .getRuntime ().availableProcessors ())+ ((((new apx . util . RangeInteger ((0 ), ((array . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array . length )- 1 ))). min )+ 1 )% (java . lang . Runtime .getRuntime ().availableProcessors ())== 0 ? 0 : 1 ))* (nbBlock )), (java . lang . Math . min (((new apx . util . RangeInteger ((0 ), ((array . length )- 1 ))). max ), ((new apx . util . RangeInteger ((0 ), ((array . length )- 1 ))). min )+ ((((new apx . util . RangeInteger ((0 ), ((array . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array . length )- 1 ))). min )+ 1 )/ (java . lang . Runtime .getRuntime ().availableProcessors ())+ ((((new apx . util . RangeInteger ((0 ), ((array . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array . length )- 1 ))). min )+ 1 )% (java . lang . Runtime .getRuntime ().availableProcessors ())== 0 ? 0 : 1 ))* ((nbBlock )+ 1 )- 1 )));
final int last0 = (range0 ). max ;
for(int block0 = (range0 ). min ;block0 <= last0 ;block0 ++ )
{
final int arrayIndex = block0 ;
{
{final Node a = array [ arrayIndex ] ;{
{

			 Node[] A_neighbors = a.getInNeighbors();
			 for ( Node b : graph_nodes2) {
freshVariable2 : do {
				 if(a==b)
					 continue freshVariable2 ;
				 float  s_AB= a.getDeltaForNode(b);
				 if(s_AB <epsilon1)
					 continue freshVariable2 ;

				 Node[] B_neighbors = b.getInNeighbors();

				 for ( Node c : A_neighbors) {
{

					 for ( Node d : B_neighbors) {
{

						 /////////////////////
						 channelPacket p = new channelPacket();
						 p.a =a;
						 p.b= b;
						 p.c = c;
						 p.d = d;
						 p.graph = graph_nodes2;
						 p.ab_score = C3 * s_AB/(c.getInDegree()*d.getInDegree()) ;
						 c.setSimRank(d, p.ab_score);
						 //[naiveChannel ! p;]

						 if(p.c==p.d)
						 {
							 p.c.setDelta(p.d, 0.0f);
							 //System.out.println("Delta SimRank (from " +x.c.m_Index+" to "+ x.d.m_Index + ")="+0);
						 }
						 else 
						 {
							 float currentTotal = p.c.getDeltaForNode(p.d);
							 //not typical equation, but simple one
							 float result =currentTotal + p.ab_score;
							 p.c.setDelta(p.d,result);
							 //System.out.println("Delta SimRank (from " +x.c.m_Index+" to "+ x.d.m_Index + ")="+result);

						 }
						 //System.out.println(""+s_AB);
					 }
				 }
}


			 }
}
		 while ( false ) ;
}
}


		 }
}}
}
}
}
}
;
branches2 .add (branch2 );
}
}
}
}
final apx . lang . gen . ExitStatus exitStatus2 =parallelBlock2 . run (branches2 );
if(exitStatus2 .hasReturned ()) {
throw new apx . lang . gen . ReturnException ();
} {
final Throwable throwable2 =exitStatus2 .thrownException ();
if(throwable2 !=null) {
if(throwable2 instanceof RuntimeException ) throw (RuntimeException )throwable2 ;
if(throwable2 instanceof Error ) throw (Error )throwable2 ;
}
}
} }
}
}
}
;
branches3 .add (branch3 );
}
final apx . lang . gen . ExitStatus exitStatus3 =parallelBlock3 . run (branches3 );
if(exitStatus3 .hasReturned ()) {
return ;
} {
final Throwable throwable3 =exitStatus3 .thrownException ();
if(throwable3 !=null) {
if(throwable3 instanceof RuntimeException ) throw (RuntimeException )throwable3 ;
if(throwable3 instanceof Error ) throw (Error )throwable3 ;
}
}
}
	}
}
	final int waitingTimeout = 50;//5 seconds
	//_________________________________
	double Enhanced_Delta_MapReduce_DeltaSimRank(Node[] graph_nodes, float C, float epsilon, int n,
			int minimumArbitraryCount,float averageThreshold, float increment)
	{
		double totalDelta =0;
		//the delta simranl implementation
		//AsyncChan<channelPacket> naiveChannel = new AsyncChan<channelPacket>();
		{
final float epsilon2 = epsilon ;
final float C4 = C ;
final Node [ ] graph_nodes3 = graph_nodes ;
final apx . lang . gen . MutableReferenceDouble totalDelta0 = new apx . lang . gen . MutableReferenceDouble (totalDelta );
final float increment0 = increment ;
final float averageThreshold0 = averageThreshold ;
final int minimumArbitraryCount0 = minimumArbitraryCount ;
{
		 final java . util . List < apx . lang . gen . Branch > branches5 =new java . util . ArrayList < apx . lang . gen . Branch > ();
final apx . lang . gen . Parallel parallelBlock5 =apx . lang . gen . Parallel .getParallelBlock ();
{
apx . lang . gen . Branch branch5 = new apx . lang . gen . Branch (){
public @java . lang .Override void run () throws java . lang . Throwable {
{
final float epsilon3 = epsilon2 ;
final float C5 = C4 ;
final Node [ ] graph_nodes4 = graph_nodes3 ;
final apx . lang . gen . MutableReferenceDouble totalDelta1 = new apx . lang . gen . MutableReferenceDouble (totalDelta0 .ref );
final float increment1 = increment0 ;
final float averageThreshold1 = averageThreshold0 ;
final int minimumArbitraryCount1 = minimumArbitraryCount0 ;
{
final Node [ ] array0 = graph_nodes4; final int blockcount0 = java . lang . Runtime .getRuntime ().availableProcessors (); {final java . util . List < apx . lang . gen . Branch > branches4 =new java . util . ArrayList < apx . lang . gen . Branch > ();
final apx . lang . gen . Parallel parallelBlock4 =apx . lang . gen . Parallel .getParallelBlock ();
{
final apx . util . RangeInteger range1 = new apx . util . RangeInteger (0 , ((blockcount0 )- 1 ));
final int last1 = (range1 ). max ;
for(int block1 = (range1 ). min ;block1 <= last1 ;block1 ++ )
{
final int nbBlock0 = block1 ;
{
{
apx . lang . gen . Branch branch4 = new apx . lang . gen . Branch (){
public @java . lang .Override void run () throws java . lang . Throwable {
{
final apx . util . RangeInteger range2 = new apx . util . RangeInteger ((((new apx . util . RangeInteger ((0 ), ((array0 . length )- 1 ))). min )+ ((((new apx . util . RangeInteger ((0 ), ((array0 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array0 . length )- 1 ))). min )+ 1 )/ (java . lang . Runtime .getRuntime ().availableProcessors ())+ ((((new apx . util . RangeInteger ((0 ), ((array0 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array0 . length )- 1 ))). min )+ 1 )% (java . lang . Runtime .getRuntime ().availableProcessors ())== 0 ? 0 : 1 ))* (nbBlock0 )), (java . lang . Math . min (((new apx . util . RangeInteger ((0 ), ((array0 . length )- 1 ))). max ), ((new apx . util . RangeInteger ((0 ), ((array0 . length )- 1 ))). min )+ ((((new apx . util . RangeInteger ((0 ), ((array0 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array0 . length )- 1 ))). min )+ 1 )/ (java . lang . Runtime .getRuntime ().availableProcessors ())+ ((((new apx . util . RangeInteger ((0 ), ((array0 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array0 . length )- 1 ))). min )+ 1 )% (java . lang . Runtime .getRuntime ().availableProcessors ())== 0 ? 0 : 1 ))* ((nbBlock0 )+ 1 )- 1 )));
final int last2 = (range2 ). max ;
for(int block2 = (range2 ). min ;block2 <= last2 ;block2 ++ )
{
final int arrayIndex0 = block2 ;
{
{final Node a = array0 [ arrayIndex0 ] ;{
{
			 if(a.CountOfAverageTimes >= minimumArbitraryCount1 && a.AverageDelta <averageThreshold1)
			 {
				 a.SkipAndIncreaseAverageDelta(increment1);

			 }
			 Node[] A_neighbors = a.getInNeighbors();
			 for ( Node b : graph_nodes4) {
freshVariable3 : do {
				 if(a==b)
					 continue freshVariable3 ;
				 float  s_AB= a.getDeltaForNode(b);
				 if(s_AB <epsilon3)
					 continue freshVariable3 ;

				 Node[] B_neighbors = b.getInNeighbors();

				 for ( Node c : A_neighbors) {
{
					 totalDelta1.ref += c.AverageDelta*c.getInDegree();
					 if(c.CountOfAverageTimes >= minimumArbitraryCount1 && c.AverageDelta <averageThreshold1)
					 {
						 c.SkipAndIncreaseAverageDelta(increment1);

					 }
					 for ( Node d : B_neighbors) {
{

						 /////////////////////
						 channelPacket p = new channelPacket();
						 p.a =a;
						 p.b= b;
						 p.c = c;
						 p.d = d;
						 p.graph = graph_nodes4;
						 p.ab_score = C5 * s_AB/(c.getInDegree()*d.getInDegree()) ;
						 c.setSimRank(d, p.ab_score);


						 //[naiveChannel ! p;]
						 if(p.c==p.d)
						 {
							 p.c.setDelta(p.d, 0.0f);
							 //System.out.println("Delta SimRank (from " +x.c.m_Index+" to "+ x.d.m_Index + ")="+0);
						 }
						 else 
						 {
							 float currentTotal = p.c.getDeltaForNode(p.d);
							 //not typical equation, but simple one
							 float result =currentTotal + p.ab_score;
							 p.c.setDelta(p.d,result);
							 //System.out.println("Delta SimRank (from " +x.c.m_Index+" to "+ x.d.m_Index + ")="+result);
						 }
						 //System.out.println(""+s_AB);
					 }
				 }
}


			 }
}
		 while ( false ) ;
}
}
		 }
}}
}
}
}
}
;
branches4 .add (branch4 );
}
}
}
}
final apx . lang . gen . ExitStatus exitStatus4 =parallelBlock4 . run (branches4 );
totalDelta0 .ref = totalDelta1 .ref ;
if(exitStatus4 .hasReturned ()) {
throw new apx . lang . gen . ReturnException (exitStatus4 .<Double >returnedValue ());
} {
final Throwable throwable4 =exitStatus4 .thrownException ();
if(throwable4 !=null) {
if(throwable4 instanceof RuntimeException ) throw (RuntimeException )throwable4 ;
if(throwable4 instanceof Error ) throw (Error )throwable4 ;
}
}
} }
}
throw new apx . lang . gen . ReturnException (totalDelta0.ref /c1 .this.m_nodes.size());
		 }
}
;
branches5 .add (branch5 );
}
final apx . lang . gen . ExitStatus exitStatus5 =parallelBlock5 . run (branches5 );
totalDelta = totalDelta0 .ref ;
if(exitStatus5 .hasReturned ()) {
return exitStatus5 .<Double >returnedValue ();
} {
final Throwable throwable5 =exitStatus5 .thrownException ();
if(throwable5 !=null) {
if(throwable5 instanceof RuntimeException ) throw (RuntimeException )throwable5 ;
if(throwable5 instanceof Error ) throw (Error )throwable5 ;
}
}
throw new apx . lang . gen . ApxError ();
}

	}
}
//_____________________________________________
	double SelectivePath_Delta_MapReduce_DeltaSimRank(Node[] graph_nodes, float C, float epsilon, int n,
			int minimumArbitraryCount,float averageThreshold, float increment)
	{
		double totalDelta =0;
		//the delta simrank implementation
		
		//AsyncChan<channelPacket> naiveChannel = new AsyncChan<channelPacket>();
		{
final Node [ ] graph_nodes5 = graph_nodes ;
final float averageThreshold2 = averageThreshold ;
final float increment2 = increment ;
final apx . lang . gen . MutableReferenceDouble totalDelta2 = new apx . lang . gen . MutableReferenceDouble (totalDelta );
final float C6 = C ;
final float epsilon4 = epsilon ;
final int minimumArbitraryCount2 = minimumArbitraryCount ;
{
		 final java . util . List < apx . lang . gen . Branch > branches8 =new java . util . ArrayList < apx . lang . gen . Branch > ();
final apx . lang . gen . Parallel parallelBlock8 =apx . lang . gen . Parallel .getParallelBlock ();
{
apx . lang . gen . Branch branch8 = new apx . lang . gen . Branch (){
public @java . lang .Override void run () throws java . lang . Throwable {
{
final Node [ ] graph_nodes6 = graph_nodes5 ;
{
final Node [ ] array1 = graph_nodes6; final int blockcount1 = java . lang . Runtime .getRuntime ().availableProcessors (); {final java . util . List < apx . lang . gen . Branch > branches6 =new java . util . ArrayList < apx . lang . gen . Branch > ();
final apx . lang . gen . Parallel parallelBlock6 =apx . lang . gen . Parallel .getParallelBlock ();
{
final apx . util . RangeInteger range3 = new apx . util . RangeInteger (0 , ((blockcount1 )- 1 ));
final int last3 = (range3 ). max ;
for(int block3 = (range3 ). min ;block3 <= last3 ;block3 ++ )
{
final int nbBlock1 = block3 ;
{
{
apx . lang . gen . Branch branch6 = new apx . lang . gen . Branch (){
public @java . lang .Override void run () throws java . lang . Throwable {
{
final apx . util . RangeInteger range4 = new apx . util . RangeInteger ((((new apx . util . RangeInteger ((0 ), ((array1 . length )- 1 ))). min )+ ((((new apx . util . RangeInteger ((0 ), ((array1 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array1 . length )- 1 ))). min )+ 1 )/ (java . lang . Runtime .getRuntime ().availableProcessors ())+ ((((new apx . util . RangeInteger ((0 ), ((array1 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array1 . length )- 1 ))). min )+ 1 )% (java . lang . Runtime .getRuntime ().availableProcessors ())== 0 ? 0 : 1 ))* (nbBlock1 )), (java . lang . Math . min (((new apx . util . RangeInteger ((0 ), ((array1 . length )- 1 ))). max ), ((new apx . util . RangeInteger ((0 ), ((array1 . length )- 1 ))). min )+ ((((new apx . util . RangeInteger ((0 ), ((array1 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array1 . length )- 1 ))). min )+ 1 )/ (java . lang . Runtime .getRuntime ().availableProcessors ())+ ((((new apx . util . RangeInteger ((0 ), ((array1 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array1 . length )- 1 ))). min )+ 1 )% (java . lang . Runtime .getRuntime ().availableProcessors ())== 0 ? 0 : 1 ))* ((nbBlock1 )+ 1 )- 1 )));
final int last4 = (range4 ). max ;
for(int block4 = (range4 ). min ;block4 <= last4 ;block4 ++ )
{
final int arrayIndex1 = block4 ;
{
{final Node a = array1 [ arrayIndex1 ] ;{
{
			 a.selectNodesAbove();
		 }
		 }
}}
}
}
}
}
;
branches6 .add (branch6 );
}
}
}
}
final apx . lang . gen . ExitStatus exitStatus6 =parallelBlock6 . run (branches6 );
if(exitStatus6 .hasReturned ()) {
throw new apx . lang . gen . ReturnException (exitStatus6 .<Double >returnedValue ());
} {
final Throwable throwable6 =exitStatus6 .thrownException ();
if(throwable6 !=null) {
if(throwable6 instanceof RuntimeException ) throw (RuntimeException )throwable6 ;
if(throwable6 instanceof Error ) throw (Error )throwable6 ;
}
}
} }
}
{
final Node [ ] graph_nodes7 = graph_nodes5 ;
final float averageThreshold3 = averageThreshold2 ;
final float increment3 = increment2 ;
final apx . lang . gen . MutableReferenceDouble totalDelta3 = new apx . lang . gen . MutableReferenceDouble (totalDelta2 .ref );
final float C7 = C6 ;
final float epsilon5 = epsilon4 ;
final int minimumArbitraryCount3 = minimumArbitraryCount2 ;
{
final Node [ ] array2 = graph_nodes7; final int blockcount2 = java . lang . Runtime .getRuntime ().availableProcessors (); {final java . util . List < apx . lang . gen . Branch > branches7 =new java . util . ArrayList < apx . lang . gen . Branch > ();
final apx . lang . gen . Parallel parallelBlock7 =apx . lang . gen . Parallel .getParallelBlock ();
{
final apx . util . RangeInteger range5 = new apx . util . RangeInteger (0 , ((blockcount2 )- 1 ));
final int last5 = (range5 ). max ;
for(int block5 = (range5 ). min ;block5 <= last5 ;block5 ++ )
{
final int nbBlock2 = block5 ;
{
{
apx . lang . gen . Branch branch7 = new apx . lang . gen . Branch (){
public @java . lang .Override void run () throws java . lang . Throwable {
{
final apx . util . RangeInteger range6 = new apx . util . RangeInteger ((((new apx . util . RangeInteger ((0 ), ((array2 . length )- 1 ))). min )+ ((((new apx . util . RangeInteger ((0 ), ((array2 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array2 . length )- 1 ))). min )+ 1 )/ (java . lang . Runtime .getRuntime ().availableProcessors ())+ ((((new apx . util . RangeInteger ((0 ), ((array2 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array2 . length )- 1 ))). min )+ 1 )% (java . lang . Runtime .getRuntime ().availableProcessors ())== 0 ? 0 : 1 ))* (nbBlock2 )), (java . lang . Math . min (((new apx . util . RangeInteger ((0 ), ((array2 . length )- 1 ))). max ), ((new apx . util . RangeInteger ((0 ), ((array2 . length )- 1 ))). min )+ ((((new apx . util . RangeInteger ((0 ), ((array2 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array2 . length )- 1 ))). min )+ 1 )/ (java . lang . Runtime .getRuntime ().availableProcessors ())+ ((((new apx . util . RangeInteger ((0 ), ((array2 . length )- 1 ))). max )- ((new apx . util . RangeInteger ((0 ), ((array2 . length )- 1 ))). min )+ 1 )% (java . lang . Runtime .getRuntime ().availableProcessors ())== 0 ? 0 : 1 ))* ((nbBlock2 )+ 1 )- 1 )));
final int last6 = (range6 ). max ;
for(int block6 = (range6 ). min ;block6 <= last6 ;block6 ++ )
{
final int arrayIndex2 = block6 ;
{
{final Node a = array2 [ arrayIndex2 ] ;{
{
//			 if(a.CountOfAverageTimes >= minimumArbitraryCount && a.AverageDelta <averageThreshold)
//			 {
//				 a.SkipAndIncreaseAverageDelta(increment);
//
//			 }
			 ArrayList<Node> A_neighborsSelective = a.m_cachedSelectedNeighbors;
			 for ( Node b : graph_nodes7) {
freshVariable4 : do {
				 if(a==b)
					 continue freshVariable4 ;
				 float  s_AB= a.getDeltaForNode(b);
				 if(s_AB <epsilon5)
					 continue freshVariable4 ;

				 ArrayList<Node> B_neighbors = b.m_cachedSelectedNeighbors;

				 for ( Node c : A_neighborsSelective) {
{
					 totalDelta3.ref += c.AverageDelta*c.getInDegree();
					 if(c.CountOfAverageTimes >= minimumArbitraryCount3 && c.AverageDelta <averageThreshold3)
					 {
						 c.SkipAndIncreaseAverageDelta(increment3);

					 }
					 for ( Node d : B_neighbors) {
{

						 /////////////////////
						 channelPacket p = new channelPacket();
						 p.a =a;
						 p.b= b;
						 p.c = c;
						 p.d = d;
						 p.graph = graph_nodes7;
						 p.ab_score = C7 * s_AB/(c.getInDegree()*d.getInDegree()) ;
						 c.setSimRank(d, p.ab_score);


						 //[naiveChannel ! p;]
						 if(p.c==p.d)
						 {
							 p.c.setDelta(p.d, 0.0f);
							 //System.out.println("Delta SimRank (from " +x.c.m_Index+" to "+ x.d.m_Index + ")="+0);
						 }
						 else 
						 {
							 float currentTotal = p.c.getDeltaForNode(p.d);
							 //not typical equation, but simple one
							 float result =currentTotal + p.ab_score;
							 p.c.setDelta(p.d,result);
							 //System.out.println("Delta SimRank (from " +x.c.m_Index+" to "+ x.d.m_Index + ")="+result);
						 }
						 //System.out.println(""+s_AB);
					 }
				 }
}


			 }
}
		 while ( false ) ;
}
}
		 }
}}
}
}
}
}
;
branches7 .add (branch7 );
}
}
}
}
final apx . lang . gen . ExitStatus exitStatus7 =parallelBlock7 . run (branches7 );
totalDelta2 .ref = totalDelta3 .ref ;
if(exitStatus7 .hasReturned ()) {
throw new apx . lang . gen . ReturnException (exitStatus7 .<Double >returnedValue ());
} {
final Throwable throwable7 =exitStatus7 .thrownException ();
if(throwable7 !=null) {
if(throwable7 instanceof RuntimeException ) throw (RuntimeException )throwable7 ;
if(throwable7 instanceof Error ) throw (Error )throwable7 ;
}
}
} }
}
throw new apx . lang . gen . ReturnException (totalDelta2.ref /c1 .this.m_nodes.size());
		 }
}
;
branches8 .add (branch8 );
}
final apx . lang . gen . ExitStatus exitStatus8 =parallelBlock8 . run (branches8 );
totalDelta = totalDelta2 .ref ;
if(exitStatus8 .hasReturned ()) {
return exitStatus8 .<Double >returnedValue ();
} {
final Throwable throwable8 =exitStatus8 .thrownException ();
if(throwable8 !=null) {
if(throwable8 instanceof RuntimeException ) throw (RuntimeException )throwable8 ;
if(throwable8 instanceof Error ) throw (Error )throwable8 ;
}
}
throw new apx . lang . gen . ApxError ();
}

	}
}


}
